package com.example.controller;


import com.alibaba.fastjson.JSON;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardResponse;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardResponseBody;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.Common;
import com.example.entity.idcar.IdCardRoot;

import com.example.util.Code;
import com.example.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

@RestController
@RequestMapping("/s/s")

public class DemoController {
    @Autowired
    private OccController controller;

    /**
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.ocr_api20210707.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // Endpoint 请参考 https://api.aliyun.com/product/ocr-api
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.ocr_api20210707.Client(config);
    }

    @RequestMapping("/ocr")
    public Object o(@RequestParam MultipartFile file) throws Exception {
//        HashMap<Object, Object> m = new HashMap<>();
//
//        Result result = controller.uploadImg(file);
//        System.out.println(result.getData());
//        m.put("tp",result.getData());
        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用，仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.ocr_api20210707.Client client = DemoController.createClient("LTAI5t9CyPUnNA586WwLG453", "7SmJyhB2bTdPa3EjLRl6mlMPUMKfcD");
        // 需要安装额外的依赖库，直接点击下载完整工程即可看到所有依赖。
        java.io.InputStream bodyStream = file.getInputStream();
        com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest recognizeIdcardRequest = new com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest()
                .setBody(bodyStream);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        String data=null;
        try {
            // 复制代码运行请自行打印 API 的返回值
            RecognizeIdcardResponse recognizeIdcardResponse = client.recognizeIdcardWithOptions(recognizeIdcardRequest, runtime);
            RecognizeIdcardResponseBody body = recognizeIdcardResponse.getBody();
            data = body.getData();
            System.out.println(data);
            IdCardRoot idCardRoot = JSON.parseObject(data, IdCardRoot.class);
//            m.put("sj",idCardRoot);
//            return new Result(Code.GET_OK,m,"成功");
            return idCardRoot;
        } catch (TeaException error) {
            // 如有需要，请打印 error
            String s = Common.assertAsString(error.message);


        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error

            String s = Common.assertAsString(error.message);
            System.out.println(s);
        }
            return data;
    }
}
